package com.liwenfeng;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class CompletableFutureTest {
    static Logger logger = LoggerFactory.getLogger("test");

    public static void main(String[] args) throws IOException {
        //1。异步执行任务
        //runAsync方法参数是Runable函数型接口，没有参数，没有返回值
        CompletableFuture.runAsync(() -> logger.info("异步操作1"));
        //supplyAsync方法参数是Supplier函数型接口，没有参数，有返回值，返回值用then方法获取
        CompletableFuture.supplyAsync(() -> {
            logger.info("异步操作2");
            return "结果";

            //thenApply和thenApplyAsync方法参数是Function函数型接口，一个参数一个返回值，可以转换结果
            //thenAccept和thenAcceptAsync方法参数是Consumer函数型接口，一个参数没有返回值，只能消费结果
        }).thenApply(List::of).thenAcceptAsync(r -> logger.info(r.toString()));
        int read = System.in.read();
        System.out.println(read);
    }
}
